#include "OCMain/Environment.h"
#include "OCMain/StringUtil.h"
#include "OCServer/ServerDef.h"

#include <grpcpp/grpcpp.h>
#include "apiinterface.grpc.pb.h"
#include "rada.grpc.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;

using namespace OCPP;

#if defined(_MSC_VER)
#ifndef OCPP_gRPC_EXPORTS
#define _gRPCExport  __declspec(dllimport)
#else
#define _gRPCExport  __declspec(dllexport)
#endif
#else
#define _gRPCExport
#endif

class radarClient {
public:
    radarClient(std::shared_ptr<Channel> channel)
        : stub_(grpc::RadaQueryService::NewStub(channel)) {}

    std::string SayHello(const std::string& user) {

        ClientContext context;
        context.AddMetadata("jwt", "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ4eHp4LXN4a2oiLCJzdWIiOiJNVVNJQy1HUlBDIiwicm9sZXMiOiJ4eHp4LXN4a2oiLCJpc3MiOiJCZWlqaW5nJ3MgQ0lNSVNTIiwiZXhwIjoxNjI1MzExMjMyLCJpYXQiOjE2MjUyMjQ4MzJ9.nHN1VDR-nVyzvJVxnA_sufOtyGSy3_TpVzazBZGK0IE");

        ::grpc::GetRadaFileByTimeRangeParam request;
        request.set_datacode("RADA_JJJ_PUZZ_CIC");
        request.set_timerange("[20230112000000,20230113000000]");
        ::grpc::RetFilesInfo reply;

        //  // The actual RPC.
        Status status =
            stub_->getRadaFileByTimeRange(&context, request, &reply);

        // Act upon its status.
        if (status.ok()) {
            return reply.fileinfos(0).filename();
        }
        else {
            std::cout << status.error_code() << ": " << status.error_message()
                << std::endl;
            return "RPC failed";
        }
    }

private:
    std::unique_ptr<grpc::RadaQueryService::Stub> stub_;
};

#ifndef OC_STATIC_LIB
extern "C" void _gRPCExport dllStartPlugin();
extern "C" void _gRPCExport dllStopPlugin();

extern "C" void _gRPCExport dllStartPlugin()
{
    radarClient greeter(grpc::CreateChannel("base.grpc.bepk.cn:80",
        grpc::InsecureChannelCredentials()));
    std::string reply = greeter.SayHello("");
    std::cout << "Greeter received: " << reply << std::endl;
}
extern "C" void _gRPCExport dllStopPlugin()
{

}

#endif
